package com.aidex.system.service.impl;

import com.aidex.common.constant.UserConstants;
import com.aidex.common.core.service.impl.BasicServiceImpl;
import com.aidex.common.exception.BizException;
import com.aidex.common.exception.CustomException;
import com.aidex.framework.cache.ConfigUtils;
import com.aidex.system.common.SysErrorCode;
import com.aidex.system.domain.SysConfig;
import com.aidex.system.mapper.SysConfigMapper;
import com.aidex.system.service.SysConfigService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class SysConfigServiceImpl extends BasicServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {

    @Override
    @Transactional
    public int insert(SysConfig sysConfig) {
        checkConfigKeyUnique(sysConfig);
        int flag = super.insert(sysConfig);
        if (flag > 0) {
            ConfigUtils.clearConfigCache(sysConfig.getConfigKey());
            ConfigUtils.setConfigCache(sysConfig);
        }
        return flag;
    }

    @Override
    @Transactional
    public int updateById(SysConfig sysConfig) {
        checkConfigKeyUnique(sysConfig);
        int flag = super.updateById(sysConfig);
        if (flag > 0) {
            ConfigUtils.clearConfigCache(sysConfig.getConfigKey());
            ConfigUtils.setConfigCache(sysConfig);
        }
        return flag;
    }

    @Override
    @Transactional
    public int deleteById(Serializable id) {
        SysConfig entity = selectById(id);
        if(entity != null) {
            if(entity.getConfigType().equals(UserConstants.YES)){
                throw new CustomException(String.format("内置参数【%1$s】不能删除 ", entity.getConfigKey()));
            }
            int flag = super.deleteById(id);
            if (flag > 0) {
                ConfigUtils.clearConfigCache(entity.getConfigKey());
            }
            return flag;
        }
        return 0;
    }

    @Override
    @Transactional
    public int deleteBatchIds(Collection<? extends Serializable> idList) {
        int flag = 0;
        if (idList != null && !idList.isEmpty()) {
            for (Serializable id : idList) {
                flag += deleteById(id);
            }
        }
        return flag;
    }

    @Override
    public void refreshCache() {
        ConfigUtils.clearConfigCache();
        loadingConfigCache();
    }

    @Override
    public void checkConfigKeyUnique(SysConfig config) {
        QueryWrapper<SysConfig> wrapper = new QueryWrapper<>();
        wrapper.eq("config_key", config.getConfigKey());
        wrapper.ne("id", config.getId());
        if (mapper.selectCount(wrapper) > 0) {
            throw new BizException(SysErrorCode.B_SYSCONFIG_ConfigKeyAlreadyExist);
        }
    }

    @Override
    public void loadingConfigCache() {
        List<SysConfig> configsList = selectList(new QueryWrapper<>());
        for (SysConfig config : configsList) {
            ConfigUtils.setConfigCache(config);
        }
    }

}
